package com.chen.pfm;


import com.chen.pfm.entity.Classify;
import com.chen.pfm.entity.OrmPfmStore;
import com.chen.pfm.utils.MyToast;
import com.chen.pfm.utils.MyUtil;
import ohos.aafwk.ability.Ability;

import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;

import java.io.FileDescriptor;

public class ClassifyDataAbility extends Ability {
    static final HiLogLabel LABEL = new HiLogLabel(3, 0xD001100, "ClassifyDataAbility");
    private static final String thisClassName = "ClassifyDataAbility";
    //表名
    private static final String tableName = "classify";

    //orm上下文
    private static OrmContext ormContext = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        HiLog.info(LABEL, thisClassName + " 已启动！");
        DatabaseHelper helper = new DatabaseHelper(this);
        ormContext = helper.getOrmContext(OrmPfmStore.DATABASE_NAME_ALIAS, OrmPfmStore.DATABASE_NAME, OrmPfmStore.class);
        if (ormContext != null) {
            HiLog.info(LABEL, "ormContext不为空");
        }
    }

    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        HiLog.info(LABEL, thisClassName + " 执行查询操作");

        if (ormContext == null) {
            HiLog.error(LABEL, thisClassName + "查询失败，ormContext为空");
            return null;
        }
        if (columns != null) {
            HiLog.error(LABEL, thisClassName + "的columns字段不为空");
            for (String s : columns) {
                HiLog.info(LABEL, "column -->" + s);
            }
        } else {
            HiLog.error(LABEL, thisClassName + "的columns字段为空");
        }
        //查询数据库
        OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, Classify.class);
        ResultSet resultSet = ormContext.query(ormPredicates, columns);
        if (resultSet == null) {
            HiLog.info(LABEL, thisClassName + "结果集为空");
        }
        return resultSet;

    }


    @Override
    public int insert(Uri uri, ValuesBucket value) {
        if (ormContext == null) {
            HiLog.error(LABEL, thisClassName + "插入失败，ormContext为空");
            return -1;
        }
        Classify classify = new Classify();
        classify.getObjectfromValues(value);
        HiLog.info(LABEL, thisClassName + "插入数据为" + classify.toString());
        int i = -1;
        if (ormContext.insert(classify.getObjectfromValues(value))) {
            ormContext.flush();
            i = 1;
            DataAbilityHelper.creator(this, uri).notifyChange(uri);
            HiLog.info(LABEL, thisClassName + "进行了插入操作，执行成功！");
        }
        HiLog.info(LABEL, "数据表" + tableName + " ===>> 插入失败");
        return i;

    }

    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        if (ormContext == null) {
            HiLog.error(LABEL, thisClassName + "删除失败，ormContext为空");
            return -1;
        }
        OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, Classify.class);
        int delete = ormContext.delete(ormPredicates);
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        HiLog.info(LABEL, thisClassName + "进行了删除操作，执行成功！");
        return delete;

    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
        if (ormContext == null) {
            HiLog.error(LABEL, thisClassName + "更新失败，ormContext为空");
            return -1;
        }
        OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, Classify.class);
        int index = ormContext.update(ormPredicates, value);
        HiLog.info(LABEL, thisClassName + " 更新值为:" + index);
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        HiLog.info(LABEL, thisClassName + "进行了删除操作，执行成功！");
        return index;
    }

    @Override
    public FileDescriptor openFile(Uri uri, String mode) {
        return null;
    }

    @Override
    public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
        return new String[0];
    }

    @Override
    public PacMap call(String method, String arg, PacMap extras) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}